// prl2.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <stdlib.h>
#include "ctprl.h"

static void printBit(unsigned char cTemp)
{
	for(int i=7;i>=0;i--)
	{
		printf("%d",getBit(&cTemp,i));
	}
	printf(" ");
}

static void test()
{
	unsigned char cTemp = 0; 
	printBit(cTemp);
	setBit(&cTemp,7,1);
	printBit(cTemp);
	setBit(&cTemp,0,1);
	printBit(cTemp);

	cTemp = 0xff;

	unsigned char cTemp2 = 0;
/*	for(int j=0;j<8;j++)
	{
		for(int i=0;i<=(8-j);i++)
		{
			getBits(&cTemp,j,i,&cTemp2);
			printBit(cTemp2);
		}
	}*/

	printf("\nStarting to test getBits\n");

	static unsigned char tArray1[3]= {0xab,0xcd,0xef};
	static unsigned char tArray2[3]= {0,0,0};
	getBits(tArray1,4,12,tArray2);

	printBit(tArray2[0]);
	printBit(tArray2[1]);
	printBit(tArray2[2]);

	printf("\t%x\t",getIntFromBits(tArray1,4,12));

	//printBit(1);
	//printBit(2);
	printf("\n");
}

int main(int argc, char* argv[])
{
	FILE* fpin; 
	FILE* fpout;

	unsigned char cTemp=0;
	unsigned char cTemp2=0;

	test();

	if(argc<2)
	{
		printf("Usage %s prl_file txt_file\n",argv[0]);
		exit(1);
	}

	fpin = fopen(argv[1],"rb");
	fpout = fopen(argv[2], "w");

	cTemp2 = fgetc(fpin);
	fprintf(fpout,"%02x",(int)cTemp2);
	cTemp = fgetc(fpin);

	int prl_a_length = (cTemp2<<8) + cTemp;

	printf("The length of the PRL 683A is: %d\n",prl_a_length);

	unsigned char* pPrl = (unsigned char*)malloc((prl_a_length-2)*sizeof(unsigned char));
	
	unsigned char* pPrlPos = pPrl;
	*pPrlPos = cTemp2;
	pPrlPos++;

	while(!feof(fpin)){
		fprintf(fpout,"%02x",(int)cTemp);
		*pPrlPos = cTemp;
		pPrlPos++;
		printBit(cTemp);
		cTemp = fgetc(fpin);
	}

	fclose(fpout);
	fclose(fpin);

	parsePRL(pPrl,prl_a_length);

	getchar();

	return 0;
}

